//
//  ImageButtonVC.swift
//  SwiftLearning
//
//  Created by 黄龙 on 2024/1/31.
//

import UIKit

class ImageButtonVC: BasicNavibarVC {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.rightBar.title = ""
    }
    
    override func initView() {
        let image = UIImage(systemName: "sun.max.fill")?.withTintColor(.label,renderingMode: .alwaysOriginal)
        var iTop = 15
        let btnWidth = 180
        let btnHeight = 50
        let button_1 = UIButton(frame: CGRect(x: 15, y: iTop, width: btnWidth, height: btnHeight))
        workView.addSubview(button_1)
        button_1.backgroundColor = .systemGreen
        button_1.setTitle("默认图文", for: .normal)
        button_1.setTitleColor(.label, for: .normal)
        button_1.titleLabel?.font = .systemFont(ofSize: 16)
        button_1.layer.cornerRadius = 6
        button_1.setImage(image, for: .normal)
        button_1.addActionWithBlock { sender in
            self.title = sender.title(for: .normal)
        }
         iTop += btnHeight + 15
        let button_2 = UIButton(frame: CGRect(x: 15, y: iTop, width: btnWidth, height: btnHeight))
        workView.addSubview(button_2)
        button_2.backgroundColor = .systemGreen
        button_2.setTitle("左图右文", for: .normal)
        button_2.setTitleColor(.label, for: .normal)
        button_2.titleLabel?.font = .systemFont(ofSize: 16)
        button_2.setImage(image, for: .normal)
        button_2.addActionWithBlock { sender in
            self.title = sender.title(for: .normal)
         }
        button_2.layoutButton(.imageLeft, space: 5.0)
        button_2.layer.cornerRadius = 6
       
        let button_3 = UIButton(frame: CGRect(x: btnWidth+30, y: iTop, width: btnWidth, height: btnHeight))
        workView.addSubview(button_3)
        button_3.backgroundColor = .systemGreen
        button_3.setTitle("右图左文", for: .normal)
        button_3.setTitleColor(.label, for: .normal)
        button_3.titleLabel?.font = .systemFont(ofSize: 16)
        button_3.setImage(image, for: .normal)
        button_3.addActionWithBlock { sender in
            self.title = sender.title(for: .normal)
         }
        button_3.layoutButton(.imageRight, space: 5.0)
        button_3.layer.cornerRadius = 6
        
        iTop += btnHeight + 15
       let button_4 = UIButton(frame: CGRect(x: 15, y: iTop, width: btnWidth, height: btnHeight))
       workView.addSubview(button_4)
        button_4.backgroundColor = .systemGreen
        button_4.setTitle("上图下文", for: .normal)
        button_4.setTitleColor(.label, for: .normal)
        button_4.titleLabel?.font = .systemFont(ofSize: 16)
        button_4.setImage(image, for: .normal)
        button_4.addActionWithBlock { sender in
            self.title = sender.title(for: .normal)
        }
        button_4.layoutButton(.imageTop, space: 5.0)
        button_4.layer.cornerRadius = 6
      
       let button_5 = UIButton(frame: CGRect(x: btnWidth+30, y: iTop, width: btnWidth, height: btnHeight))
       workView.addSubview(button_5)
        button_5.backgroundColor = .systemGreen
        button_5.setTitle("下图上文", for: .normal)
        button_5.setTitleColor(.label, for: .normal)
        button_5.titleLabel?.font = .systemFont(ofSize: 16)
        button_5.setImage(image, for: .normal)
        button_5.addActionWithBlock { sender in
            self.title = sender.title(for: .normal)
        }
       button_5.layoutButton(.imageBottom, space: 5.0)
       button_5.layer.cornerRadius = 6
        
        iTop += btnHeight + 35
        let button_6 = UIButton(frame: CGRect(x: 15, y: iTop, width: btnWidth, height: btnHeight))
        workView.addSubview(button_6)
        button_6.backgroundColor = .systemGreen
        button_6.setTitle("线形渐变背景", for: .normal)
        button_6.setTitleColor(.label, for: .normal)
        button_6.titleLabel?.font = .systemFont(ofSize: 16)
        button_6.setImage(image, for: .normal)
        button_6.addActionWithBlock { sender in
             self.title = sender.title(for: .normal)
        }
        button_6.layoutButton(.imageBottom, space: 5.0)
        let _ = button_6.gradientLayer(With: [UIColor.systemPink.cgColor,UIColor.systemGreen.cgColor,UIColor.systemYellow.cgColor])
//        button_6.layer.cornerRadius = 6 //gradientLayer会让cornerRadius无效,改用下面方法：
        button_6.easyRoundRect(corner: [.allCorners], radii: CGSize(width: 6.0, height: 6.0))
        
        let button_7 = UIButton(frame: CGRect(x: btnWidth+30, y: iTop, width: btnWidth, height: btnHeight))
        workView.addSubview(button_7)
        button_7.backgroundColor = .systemGreen
        button_7.setTitle("径向渐变背景", for: .normal)
        button_7.setTitleColor(.label, for: .normal)
        button_7.titleLabel?.font = .systemFont(ofSize: 16)
        button_7.setImage(image, for: .normal)
        button_7.addActionWithBlock { sender in
             self.title = sender.title(for: .normal)
        }
        button_7.layoutButton(.imageTop, space: 5.0)
        button_7.layer.cornerRadius = 6
        let _ = button_7.radialGradientLayer(with: [UIColor.systemPink.cgColor,UIColor.systemGreen.cgColor,UIColor.systemYellow.cgColor],startPos: CGPoint(x: 0.5, y: 0.5),endPos: CGPoint(x: 1.0, y: 1.0))
        
        iTop += btnHeight + 15
        let button_8 = UIButton(frame: CGRect(x: 15, y: iTop, width: btnWidth, height: btnHeight))
        workView.addSubview(button_8)
        button_8.backgroundColor = .systemGreen
        button_8.setTitle("锥形渐变背景", for: .normal)
        button_8.setTitleColor(.label, for: .normal)
        button_8.titleLabel?.font = .systemFont(ofSize: 16)
        button_8.setImage(image, for: .normal)
        button_8.addActionWithBlock { sender in
             self.title = sender.title(for: .normal)
         }
        button_8.layoutButton(.imageBottom, space: 5.0)
        button_8.layer.cornerRadius = 6
        let _ = button_8.angularGradientLayer(with: [UIColor.systemPink.cgColor,UIColor.systemGreen.cgColor,UIColor.systemBlue.cgColor], startPos: CGPoint(x: 0.5, y: 0.5), endPos: CGPoint(x: 1.0, y: 1.0))
        
        iTop += btnHeight + 15
        let button_9 = UIButton(frame: CGRect(x: 15, y: iTop, width: btnWidth, height: btnHeight))
        workView.addSubview(button_9)
//        button_9.backgroundColor = .systemPink
        button_9.setTitle("平铺图片背景1", for: .normal)
        button_9.setTitleColor(.label, for: .normal)
        button_9.titleLabel?.font = .systemFont(ofSize: 16)
        button_9.setImage(image, for: .normal)
        button_9.addActionWithBlock { sender in
             self.title = sender.title(for: .normal)
         }
        button_9.layoutButton(.imageBottom, space: 5.0)
        button_9.layer.cornerRadius = 6
        let bgImage = UIImage(named: "button_bg_2@2x.png")
        //从源图截取指区域进行背景平铺
        button_9.setBackgroundImage(bgImage?.tileOrStretchImageInsets(), for: .normal)
      
        let button_a = UIButton(frame: CGRect(x: btnWidth+30, y: iTop, width: btnWidth, height: btnHeight))
        workView.addSubview(button_a)
//        button_a.backgroundColor = .systemPink
        button_a.setTitle("平铺图片背景2", for: .normal)
        button_a.setTitleColor(.label, for: .normal)
        button_a.titleLabel?.font = .systemFont(ofSize: 16)
        button_a.setImage(image, for: .normal)
        button_a.addActionWithBlock { sender in
             self.title = sender.title(for: .normal)
        }
        button_a.layoutButton(.imageTop, space: 5.0)
        button_a.layer.cornerRadius = 6
        let bgImage_a = UIImage(named: "button_bg_1@2x.png")
        //从源图截取指区域进行背景平铺
        button_a.setBackgroundImage(bgImage_a?.tileOrStretchImageInsets(capInsets: UIEdgeInsets(top: 2, left: 2, bottom: 2, right: 2)), for: .normal)
        
        iTop += btnHeight + 15
        let button_b = UIButton(frame: CGRect(x: 15, y: iTop, width: btnWidth, height: btnHeight))
        workView.addSubview(button_b)
//        button_b.backgroundColor = .systemPink
        button_b.setTitle("拉伸图片背景1", for: .normal)
        button_b.setTitleColor(.label, for: .normal)
        button_b.titleLabel?.font = .systemFont(ofSize: 16)
        button_b.setImage(image, for: .normal)
        button_b.addActionWithBlock { sender in
             self.title = sender.title(for: .normal)
         }
        button_b.layoutButton(.imageBottom, space: 5.0)
        button_b.layer.cornerRadius = 6
        let bgImage_b = UIImage(named: "button_bg_2@2x.png")
        //从源图截取指区域进行背景平铺
        button_b.setBackgroundImage(bgImage_b?.tileOrStretchImageInsets(resizingMode: .stretch), for: .normal)
        
        let button_c = UIButton(frame: CGRect(x: btnWidth+30, y: iTop, width: btnWidth, height: btnHeight))
        workView.addSubview(button_c)
//        button_c.backgroundColor = .systemPink
        button_c.setTitle("拉伸图片背景2", for: .normal)
        button_c.setTitleColor(.label, for: .normal)
        button_c.titleLabel?.font = .systemFont(ofSize: 16)
        button_c.setImage(image, for: .normal)
        button_c.addActionWithBlock { sender in
             self.title = sender.title(for: .normal)
        }
        button_c.layoutButton(.imageTop, space: 5.0)
        let bgImage_c = UIImage(named: "button_bg@2x.png")
        //从源图截取指区域进行背景平铺
        button_c.setBackgroundImage(bgImage_c?.tileOrStretchImageInsets(capInsets: UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10),resizingMode: .stretch), for: .normal)
//        button_c.layer.cornerRadius = 6//设置BackgroundImage，会导致cornerRadius设置圆角不起作用，改用：
        button_c.easyRoundRect(corner: [.allCorners], radii: CGSize(width: 6.0, height: 6.0))
    }
    

    
//
}
